﻿

{
    //db.ScoringMatrixInput.aggregate([

    list:[
    // we assume we have indicator scoring inserted
    {
        // compute topics  as sum(indicator scoring * indicator weight)

        $project:
            {
                // copy ids data 
                _id: "$_id",
                CompanyId: "$CompanyId",
                MatrixId: "$MatrixId",
                PeerGroupId: "$PeerGroupId",

                //ParentGroupId: "$ParentGroupId",
                //PeerGroupName: "$PeerGroupName",
                //WorkingVersion: "$WorkingVersion",
                //CompanyName: "$CompanyName",
                //CompanyScore: "$CompanyScore",

                // copy indicators

                //G_1_1: '$G_1_1',
                //G_1_1_1: '$G_1_1_1',
                //G_1_2: '$G_1_2',
                //G_1_3: '$G_1_3',
                //G_1_3_1: '$G_1_3_1',
                //G_1_3_2: '$G_1_3_2',
                //G_1_3_3: '$G_1_3_3',
                //G_1_3_4: '$G_1_3_4',
                //G_1_3_5: '$G_1_3_5',
                //G_1_4: '$G_1_4',
                //G_1_4_1: '$G_1_4_1',
                //G_1_4_3: '$G_1_4_3',
                //G_1_4_4: '$G_1_4_4',
                //G_1_4_5: '$G_1_4_5',
                //G_1_4_6: '$G_1_4_6',
                //G_1_5: '$G_1_5',
                //G_2_1: '$G_2_1',
                //G_2_2: '$G_2_2',
                //G_2_3: '$G_2_3',
                //G_2_4: '$G_2_4',
                //G_2_5: '$G_2_5',
                //G_2_5_1: '$G_2_5_1',
                //G_2_6: '$G_2_6',
                //G_2_7: '$G_2_7',
                //G_2_8: '$G_2_8',
                //G_2_9: '$G_2_9',
                //G_2_10: '$G_2_10',
                //G_2_11: '$G_2_11',
                //G_2_12: '$G_2_12',
                //G_2_13: '$G_2_13',
                //G_3_1: '$G_3_1',
                //G_3_2: '$G_3_2',
                //G_3_3_1: '$G_3_3_1',
                //G_3_4: '$G_3_4',
                //S_1_1: '$S_1_1',
                //S_1_1_1: '$S_1_1_1',
                //S_1_2: '$S_1_2',
                //S_1_3: '$S_1_3',
                //S_1_4: '$S_1_4',
                //S_1_5: '$S_1_5',
                //S_1_5_1: '$S_1_5_1',
                //S_1_6: '$S_1_6',
                //S_1_6_1: '$S_1_6_1',
                //S_1_6_2: '$S_1_6_2',
                //S_1_6_3: '$S_1_6_3',
                //S_1_6_4: '$S_1_6_4',
                //S_1_6_5: '$S_1_6_5',
                //S_1_6_6: '$S_1_6_6',
                //S_1_7: '$S_1_7',
                //S_2_1: '$S_2_1',
                //S_2_1_1: '$S_2_1_1',
                //S_2_1_2: '$S_2_1_2',
                //S_2_1_3: '$S_2_1_3',
                //S_2_2: '$S_2_2',
                //S_2_2_1: '$S_2_2_1',
                //S_2_2_2: '$S_2_2_2',
                //S_2_2_3: '$S_2_2_3',
                //S_2_2_4: '$S_2_2_4',
                //S_2_3: '$S_2_3',
                //S_3_1_1: '$S_3_1_1',
                //S_3_1_2: '$S_3_1_2',
                //S_3_1_3: '$S_3_1_3',
                //S_3_1_4: '$S_3_1_4',
                //S_3_1_5: '$S_3_1_5',
                //S_3_1_6: '$S_3_1_6',
                //S_3_1_7: '$S_3_1_7',
                //S_3_1_8: '$S_3_1_8',
                //S_3_1_9: '$S_3_1_9',
                //S_3_1_10: '$S_3_1_10',
                //S_3_1_11: '$S_3_1_11',
                //S_3_1_12: '$S_3_1_12',
                //S_3_2_1: '$S_3_2_1',
                //S_3_3: '$S_3_3',
                //S_4_1: '$S_4_1',
                //S_4_2_1: '$S_4_2_1',
                //S_4_2_2: '$S_4_2_2',
                //S_4_2_3: '$S_4_2_3',
                //S_4_2_4: '$S_4_2_4',
                //S_4_2_5: '$S_4_2_5',
                //S_4_2_6: '$S_4_2_6',
                //S_4_2_7: '$S_4_2_7',
                //S_4_2_8: '$S_4_2_8',
                //S_4_2_9: '$S_4_2_9',
                //S_4_2_10: '$S_4_2_10',
                //S_4_2_11: '$S_4_2_11',
                //S_4_2_12: '$S_4_2_12',
                //S_4_2_13: '$S_4_2_13',
                //S_4_2_14: '$S_4_2_14',
                //S_4_3: '$S_4_3',
                //S_5_1: '$S_5_1',
                //S_5_2: '$S_5_2',
                //S_5_3: '$S_5_3',
                //E_1_1: '$E_1_1',
                //E_1_1_1: '$E_1_1_1',
                //E_1_2: '$E_1_2',
                //E_1_2_1: '$E_1_2_1',
                //E_1_2_2: '$E_1_2_2',
                //E_1_2_3: '$E_1_2_3',
                //E_1_2_4: '$E_1_2_4',
                //E_1_2_6: '$E_1_2_6',
                //E_1_2_7: '$E_1_2_7',
                //E_1_2_8: '$E_1_2_8',
                //E_1_3: '$E_1_3',
                //E_1_3_2: '$E_1_3_2',
                //E_1_3_3: '$E_1_3_3',
                //E_1_3_4: '$E_1_3_4',
                //E_1_3_5: '$E_1_3_5',
                //E_1_4: '$E_1_4',
                //E_1_5: '$E_1_5',
                //E_1_6: '$E_1_6',
                //E_1_7: '$E_1_7',
                //E_1_7_1: '$E_1_7_1',
                //E_1_7_2: '$E_1_7_2',
                //E_1_8: '$E_1_8',
                //E_1_9: '$E_1_9',
                //E_1_10: '$E_1_10',
                //E_1_11: '$E_1_11',
                //E_1_12: '$E_1_12',
                //E_2_1: '$E_2_1',
                //E_2_1_1: '$E_2_1_1',
                //E_2_1_2: '$E_2_1_2',
                //E_2_1_3: '$E_2_1_3',
                //E_2_1_4: '$E_2_1_4',
                //E_2_1_5: '$E_2_1_5',
                //E_2_1_6: '$E_2_1_6',
                //E_2_1_7: '$E_2_1_7',
                //E_2_1_8: '$E_2_1_8',
                //E_2_1_9: '$E_2_1_9',
                //E_2_1_10: '$E_2_1_10',
                //E_2_2: '$E_2_2',
                //E_3_1_1: '$E_3_1_1',
                //E_3_1_2: '$E_3_1_2',
                //E_3_1_3: '$E_3_1_3',
                //E_3_1_4: '$E_3_1_4',
                //E_3_1_5: '$E_3_1_5',
                //E_3_1_6: '$E_3_1_6',
                //E_3_1_7: '$E_3_1_7',
                //E_3_1_8: '$E_3_1_8',
                //E_3_1_9: '$E_3_1_9',
                //E_3_1_10: '$E_3_1_10',
                //E_3_1_11: '$E_3_1_11',
                //E_3_1_12: '$E_3_1_12',
                //E_3_1_13: '$E_3_1_13',
                //E_3_1_14: '$E_3_1_14',
                //E_3_1_15: '$E_3_1_15',
                //E_3_1_16: '$E_3_1_16',
                //E_3_1_17: '$E_3_1_17',
                //E_3_2: '$E_3_2',
                //P_1: '$P_1',
                //P_2: '$P_2',
                //P_3: '$P_3',
                //P_4: '$P_4',
                //P_5: '$P_5',
                //P_6: '$P_6',
                //P_7: '$P_7',
                //P_8: '$P_8',
                //P_9: '$P_9',
                //P_10: '$P_10',
                //P_11: '$P_11',
                //P_12: '$P_12',

                // copy themes 
                E: "$E",
                S: "$S",
                G: "$G",
                P: "$P",


                // compute topics
                E_1: {
                    Weight: "$E_1.Weight",
                    Score: {
                        $add: [
                              { $cond: [{ $eq: ['$E_1_1', null] }, 0, { $multiply: ['$E_1_1.Score', '$E_1_1.Weight'] }] },
                              { $ifNull: [{ $multiply: ['$E_1_1_1.Score', '$E_1_1_1.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_2.Score', '$E_1_2.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_2_1.Score', '$E_1_2_1.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_2_2.Score', '$E_1_2_2.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_2_3.Score', '$E_1_2_3.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_2_4.Score', '$E_1_2_4.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_2_6.Score', '$E_1_2_6.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_2_7.Score', '$E_1_2_7.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_2_8.Score', '$E_1_2_8.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_3.Score', '$E_1_3.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_3_2.Score', '$E_1_3_2.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_3_3.Score', '$E_1_3_3.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_3_4.Score', '$E_1_3_4.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_3_5.Score', '$E_1_3_5.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_4.Score', '$E_1_4.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_5.Score', '$E_1_5.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_6.Score', '$E_1_6.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_7.Score', '$E_1_7.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_7_1.Score', '$E_1_7_1.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_7_2.Score', '$E_1_7_2.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_8.Score', '$E_1_8.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_9.Score', '$E_1_9.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_10.Score', '$E_1_10.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_11.Score', '$E_1_11.Weight'] }, 0] },
                              { $ifNull: [{ $multiply: ['$E_1_12.Score', '$E_1_12.Weight'] }, 0] },
                        ]
                    }
                },
                E_2: {
                    Weight: "$E_2.Weight",
                    Score: {
                        $add: [
                                 { $ifNull: [{ $multiply: ['$E_2_1.Score', '$E_2_1.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_2_1_1.Score', '$E_2_1_1.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_2_1_2.Score', '$E_2_1_2.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_2_1_3.Score', '$E_2_1_3.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_2_1_4.Score', '$E_2_1_4.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_2_1_5.Score', '$E_2_1_5.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_2_1_6.Score', '$E_2_1_6.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_2_1_7.Score', '$E_2_1_7.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_2_1_8.Score', '$E_2_1_8.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_2_1_9.Score', '$E_2_1_9.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_2_1_10.Score', '$E_2_1_10.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_2_2.Score', '$E_2_2.Weight'] }, 0] },
                        ]
                    }
                },

                E_3: {
                    Weight: "$E_3.Weight",
                    Score: {
                        $add: [
                                 { $ifNull: [{ $multiply: ['$E_3_1_1.Score', '$E_3_1_1.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_2.Score', '$E_3_1_2.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_3.Score', '$E_3_1_3.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_4.Score', '$E_3_1_4.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_5.Score', '$E_3_1_5.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_6.Score', '$E_3_1_6.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_7.Score', '$E_3_1_7.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_8.Score', '$E_3_1_8.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_9.Score', '$E_3_1_9.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_10.Score', '$E_3_1_10.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_11.Score', '$E_3_1_11.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_12.Score', '$E_3_1_12.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_13.Score', '$E_3_1_13.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_14.Score', '$E_3_1_14.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_15.Score', '$E_3_1_15.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_16.Score', '$E_3_1_16.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_1_17.Score', '$E_3_1_17.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$E_3_2.Score', '$E_3_2.Weight'] }, 0] },

                        ]
                    }
                },



                G_1: {
                    Weight: "$G_1.Weight",
                    Score: {
                        $add: [
                             { $ifNull: [{ $multiply: ['$G_1_1.Score', '$G_1_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_1_1_1.Score', '$G_1_1_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_1_2.Score', '$G_1_2.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_1_3.Score', '$G_1_3.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_1_3_1.Score', '$G_1_3_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_1_3_2.Score', '$G_1_3_2.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_1_3_3.Score', '$G_1_3_3.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_1_3_4.Score', '$G_1_3_4.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_1_3_5.Score', '$G_1_3_5.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_1_4.Score', '$G_1_4.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_1_4_1.Score', '$G_1_4_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_1_4_3.Score', '$G_1_4_3.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_1_4_4.Score', '$G_1_4_4.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_1_4_5.Score', '$G_1_4_5.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_1_4_6.Score', '$G_1_4_6.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_1_5.Score', '$G_1_5.Weight'] }, 0] },
                        ]
                    }
                },
                G_2: {
                    Weight: "$G_2.Weight",
                    Score: {
                        $add: [
                                 { $ifNull: [{ $multiply: ['$G_2_1.Score', '$G_2_1.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$G_2_2.Score', '$G_2_2.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$G_2_3.Score', '$G_2_3.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$G_2_4.Score', '$G_2_4.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$G_2_5.Score', '$G_2_5.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$G_2_5_1.Score', '$G_2_5_1.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$G_2_6.Score', '$G_2_6.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$G_2_7.Score', '$G_2_7.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$G_2_8.Score', '$G_2_8.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$G_2_9.Score', '$G_2_9.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$G_2_10.Score', '$G_2_10.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$G_2_11.Score', '$G_2_11.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$G_2_12.Score', '$G_2_12.Weight'] }, 0] },
                                 { $ifNull: [{ $multiply: ['$G_2_13.Score', '$G_2_13.Weight'] }, 0] },
                        ]
                    }
                },

                G_3: {
                    Weight: "$G_3.Weight",
                    Score: {
                        $add: [
                             { $ifNull: [{ $multiply: ['$G_3_1.Score', '$G_3_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_3_2.Score', '$G_3_2.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_3_3_1.Score', '$G_3_3_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$G_3_4.Score', '$G_3_4.Weight'] }, 0] },
                        ]
                    }
                },
                S_1: {
                    Weight: "$S_1.Weight",
                    Score: {
                        $add: [
                             { $ifNull: [{ $multiply: ['$S_1_1.Score', '$S_1_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_1_1_1.Score', '$S_1_1_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_1_2.Score', '$S_1_2.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_1_3.Score', '$S_1_3.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_1_4.Score', '$S_1_4.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_1_5.Score', '$S_1_5.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_1_5_1.Score', '$S_1_5_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_1_6.Score', '$S_1_6.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_1_6_1.Score', '$S_1_6_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_1_6_2.Score', '$S_1_6_2.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_1_6_3.Score', '$S_1_6_3.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_1_6_4.Score', '$S_1_6_4.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_1_6_5.Score', '$S_1_6_5.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_1_6_6.Score', '$S_1_6_6.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_1_7.Score', '$S_1_7.Weight'] }, 0] },
                        ]
                    }
                },
                S_2: {
                    Weight: "$S_2.Weight",
                    Score: {
                        $add: [
                             { $ifNull: [{ $multiply: ['$S_2_1.Score', '$S_2_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_2_1_1.Score', '$S_2_1_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_2_1_2.Score', '$S_2_1_2.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_2_1_3.Score', '$S_2_1_3.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_2_2.Score', '$S_2_2.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_2_2_1.Score', '$S_2_2_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_2_2_2.Score', '$S_2_2_2.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_2_2_3.Score', '$S_2_2_3.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_2_2_4.Score', '$S_2_2_4.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_2_3.Score', '$S_2_3.Weight'] }, 0] },
                        ]
                    }
                },

                S_3: {
                    Weight: "$S_3.Weight",
                    Score: {
                        $add: [
                             { $ifNull: [{ $multiply: ['$S_3_1_1.Score', '$S_3_1_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_3_1_2.Score', '$S_3_1_2.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_3_1_3.Score', '$S_3_1_3.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_3_1_4.Score', '$S_3_1_4.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_3_1_5.Score', '$S_3_1_5.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_3_1_6.Score', '$S_3_1_6.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_3_1_7.Score', '$S_3_1_7.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_3_1_8.Score', '$S_3_1_8.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_3_1_9.Score', '$S_3_1_9.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_3_1_10.Score', '$S_3_1_10.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_3_1_11.Score', '$S_3_1_11.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_3_1_12.Score', '$S_3_1_12.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_3_2_1.Score', '$S_3_2_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_3_3.Score', '$S_3_3.Weight'] }, 0] },
                        ]
                    }
                },
                S_4: {
                    Weight: "$S_4.Weight",
                    Score: {
                        $add: [
                             { $ifNull: [{ $multiply: ['$S_4_1.Score', '$S_4_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_4_2_1.Score', '$S_4_2_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_4_2_2.Score', '$S_4_2_2.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_4_2_3.Score', '$S_4_2_3.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_4_2_4.Score', '$S_4_2_4.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_4_2_5.Score', '$S_4_2_5.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_4_2_6.Score', '$S_4_2_6.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_4_2_7.Score', '$S_4_2_7.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_4_2_8.Score', '$S_4_2_8.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_4_2_9.Score', '$S_4_2_9.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_4_2_10.Score', '$S_4_2_10.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_4_2_11.Score', '$S_4_2_11.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_4_2_12.Score', '$S_4_2_12.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_4_2_13.Score', '$S_4_2_13.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_4_2_14.Score', '$S_4_2_14.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_4_3.Score', '$S_4_3.Weight'] }, 0] },
                        ]
                    }
                },
                S_5: {
                    Weight: "$S_5.Weight",
                    Score: {
                        $add: [
                             { $ifNull: [{ $multiply: ['$S_5_1.Score', '$S_5_1.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_5_2.Score', '$S_5_2.Weight'] }, 0] },
                             { $ifNull: [{ $multiply: ['$S_5_3.Score', '$S_5_3.Weight'] }, 0] },
                        ]
                    }
                },
            }
    }
    ,
    {// compute themes scoring
        $project: {
            // copy ids data 
            _id: "$_id",
            CompanyId: "$CompanyId",
            MatrixId: "$MatrixId",
            PeerGroupId: "$PeerGroupId",

            //ParentGroupId: "$ParentGroupId",
            //PeerGroupName: "$PeerGroupName",
            //WorkingVersion: "$WorkingVersion",
            //CompanyName: "$CompanyName",
            //CompanyScore: "$CompanyScore",

            //// copy indicators

            //G_1_1: '$G_1_1',
            //G_1_1_1: '$G_1_1_1',
            //G_1_2: '$G_1_2',
            //G_1_3: '$G_1_3',
            //G_1_3_1: '$G_1_3_1',
            //G_1_3_2: '$G_1_3_2',
            //G_1_3_3: '$G_1_3_3',
            //G_1_3_4: '$G_1_3_4',
            //G_1_3_5: '$G_1_3_5',
            //G_1_4: '$G_1_4',
            //G_1_4_1: '$G_1_4_1',
            //G_1_4_3: '$G_1_4_3',
            //G_1_4_4: '$G_1_4_4',
            //G_1_4_5: '$G_1_4_5',
            //G_1_4_6: '$G_1_4_6',
            //G_1_5: '$G_1_5',
            //G_2_1: '$G_2_1',
            //G_2_2: '$G_2_2',
            //G_2_3: '$G_2_3',
            //G_2_4: '$G_2_4',
            //G_2_5: '$G_2_5',
            //G_2_5_1: '$G_2_5_1',
            //G_2_6: '$G_2_6',
            //G_2_7: '$G_2_7',
            //G_2_8: '$G_2_8',
            //G_2_9: '$G_2_9',
            //G_2_10: '$G_2_10',
            //G_2_11: '$G_2_11',
            //G_2_12: '$G_2_12',
            //G_2_13: '$G_2_13',
            //G_3_1: '$G_3_1',
            //G_3_2: '$G_3_2',
            //G_3_3_1: '$G_3_3_1',
            //G_3_4: '$G_3_4',
            //S_1_1: '$S_1_1',
            //S_1_1_1: '$S_1_1_1',
            //S_1_2: '$S_1_2',
            //S_1_3: '$S_1_3',
            //S_1_4: '$S_1_4',
            //S_1_5: '$S_1_5',
            //S_1_5_1: '$S_1_5_1',
            //S_1_6: '$S_1_6',
            //S_1_6_1: '$S_1_6_1',
            //S_1_6_2: '$S_1_6_2',
            //S_1_6_3: '$S_1_6_3',
            //S_1_6_4: '$S_1_6_4',
            //S_1_6_5: '$S_1_6_5',
            //S_1_6_6: '$S_1_6_6',
            //S_1_7: '$S_1_7',
            //S_2_1: '$S_2_1',
            //S_2_1_1: '$S_2_1_1',
            //S_2_1_2: '$S_2_1_2',
            //S_2_1_3: '$S_2_1_3',
            //S_2_2: '$S_2_2',
            //S_2_2_1: '$S_2_2_1',
            //S_2_2_2: '$S_2_2_2',
            //S_2_2_3: '$S_2_2_3',
            //S_2_2_4: '$S_2_2_4',
            //S_2_3: '$S_2_3',
            //S_3_1_1: '$S_3_1_1',
            //S_3_1_2: '$S_3_1_2',
            //S_3_1_3: '$S_3_1_3',
            //S_3_1_4: '$S_3_1_4',
            //S_3_1_5: '$S_3_1_5',
            //S_3_1_6: '$S_3_1_6',
            //S_3_1_7: '$S_3_1_7',
            //S_3_1_8: '$S_3_1_8',
            //S_3_1_9: '$S_3_1_9',
            //S_3_1_10: '$S_3_1_10',
            //S_3_1_11: '$S_3_1_11',
            //S_3_1_12: '$S_3_1_12',
            //S_3_2_1: '$S_3_2_1',
            //S_3_3: '$S_3_3',
            //S_4_1: '$S_4_1',
            //S_4_2_1: '$S_4_2_1',
            //S_4_2_2: '$S_4_2_2',
            //S_4_2_3: '$S_4_2_3',
            //S_4_2_4: '$S_4_2_4',
            //S_4_2_5: '$S_4_2_5',
            //S_4_2_6: '$S_4_2_6',
            //S_4_2_7: '$S_4_2_7',
            //S_4_2_8: '$S_4_2_8',
            //S_4_2_9: '$S_4_2_9',
            //S_4_2_10: '$S_4_2_10',
            //S_4_2_11: '$S_4_2_11',
            //S_4_2_12: '$S_4_2_12',
            //S_4_2_13: '$S_4_2_13',
            //S_4_2_14: '$S_4_2_14',
            //S_4_3: '$S_4_3',
            //S_5_1: '$S_5_1',
            //S_5_2: '$S_5_2',
            //S_5_3: '$S_5_3',
            //E_1_1: '$E_1_1',
            //E_1_1_1: '$E_1_1_1',
            //E_1_2: '$E_1_2',
            //E_1_2_1: '$E_1_2_1',
            //E_1_2_2: '$E_1_2_2',
            //E_1_2_3: '$E_1_2_3',
            //E_1_2_4: '$E_1_2_4',
            //E_1_2_6: '$E_1_2_6',
            //E_1_2_7: '$E_1_2_7',
            //E_1_2_8: '$E_1_2_8',
            //E_1_3: '$E_1_3',
            //E_1_3_2: '$E_1_3_2',
            //E_1_3_3: '$E_1_3_3',
            //E_1_3_4: '$E_1_3_4',
            //E_1_3_5: '$E_1_3_5',
            //E_1_4: '$E_1_4',
            //E_1_5: '$E_1_5',
            //E_1_6: '$E_1_6',
            //E_1_7: '$E_1_7',
            //E_1_7_1: '$E_1_7_1',
            //E_1_7_2: '$E_1_7_2',
            //E_1_8: '$E_1_8',
            //E_1_9: '$E_1_9',
            //E_1_10: '$E_1_10',
            //E_1_11: '$E_1_11',
            //E_1_12: '$E_1_12',
            //E_2_1: '$E_2_1',
            //E_2_1_1: '$E_2_1_1',
            //E_2_1_2: '$E_2_1_2',
            //E_2_1_3: '$E_2_1_3',
            //E_2_1_4: '$E_2_1_4',
            //E_2_1_5: '$E_2_1_5',
            //E_2_1_6: '$E_2_1_6',
            //E_2_1_7: '$E_2_1_7',
            //E_2_1_8: '$E_2_1_8',
            //E_2_1_9: '$E_2_1_9',
            //E_2_1_10: '$E_2_1_10',
            //E_2_2: '$E_2_2',
            //E_3_1_1: '$E_3_1_1',
            //E_3_1_2: '$E_3_1_2',
            //E_3_1_3: '$E_3_1_3',
            //E_3_1_4: '$E_3_1_4',
            //E_3_1_5: '$E_3_1_5',
            //E_3_1_6: '$E_3_1_6',
            //E_3_1_7: '$E_3_1_7',
            //E_3_1_8: '$E_3_1_8',
            //E_3_1_9: '$E_3_1_9',
            //E_3_1_10: '$E_3_1_10',
            //E_3_1_11: '$E_3_1_11',
            //E_3_1_12: '$E_3_1_12',
            //E_3_1_13: '$E_3_1_13',
            //E_3_1_14: '$E_3_1_14',
            //E_3_1_15: '$E_3_1_15',
            //E_3_1_16: '$E_3_1_16',
            //E_3_1_17: '$E_3_1_17',
            //E_3_2: '$E_3_2',
            //P_1: '$P_1',
            //P_2: '$P_2',
            //P_3: '$P_3',
            //P_4: '$P_4',
            //P_5: '$P_5',
            //P_6: '$P_6',
            //P_7: '$P_7',
            //P_8: '$P_8',
            //P_9: '$P_9',
            //P_10: '$P_10',
            //P_11: '$P_11',
            //P_12: '$P_12',



            // copy topics 
            E_1: "$E_1",
            E_2: "$E_2",
            E_3: "$E_3",

            G_1: "$G_1",
            G_2: "$G_2",
            G_3: "$G_3",

            S_1: "$S_1",
            S_2: "$S_2",
            S_3: "$S_3",
            S_4: "$S_4",
            S_5: "$S_5",

            // compute themes
            E: {
                Weight: "$E.Weight",
                Score: {
                    $add: [
                          { $ifNull: [{ $multiply: [.001, '$E_1.Score', '$E_1.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$E_2.Score', '$E_2.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$E_3.Score', '$E_3.Weight'] }, 0] },
                    ]
                }
            },
            G: {
                Weight: "$G.Weight",
                Score: {
                    $add: [
                          { $ifNull: [{ $multiply: [.001, '$G_1.Score', '$G_1.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$G_2.Score', '$G_2.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$G_3.Score', '$G_3.Weight'] }, 0] },
                    ]
                }
            },
            S: {
                Weight: "$S.Weight",
                Score: {
                    $add: [
                          { $ifNull: [{ $multiply: [.001, '$S_1.Score', '$S_1.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$S_2.Score', '$S_2.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$S_3.Score', '$S_3.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$S_4.Score', '$S_4.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$S_5.Score', '$S_5.Weight'] }, 0] },
                    ]
                }
            },
            P: {
                Weight: "$P.Weight",
                Score: {
                    $add: [
                          { $ifNull: [{ $multiply: [.001, '$P_1.Score', '$P_1.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$P_2.Score', '$P_2.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$P_3.Score', '$P_3.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$P_4.Score', '$P_4.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$P_5.Score', '$P_5.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$P_6.Score', '$P_6.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$P_7.Score', '$P_7.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$P_8.Score', '$P_8.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$P_9.Score', '$P_9.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$P_10.Score', '$P_10.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$P_11.Score', '$P_11.Weight'] }, 0] },
                          { $ifNull: [{ $multiply: [.001, '$P_12.Score', '$P_12.Weight'] }, 0] },
                    ]
                }
            },

        }
    }
     ,
    {
        // compute total score
        $project:
            {
                // copy ids data 
                _id: "$_id",
                CompanyId: "$CompanyId",
                MatrixId: "$MatrixId",
                PeerGroupId: "$PeerGroupId",

                //ParentGroupId: "$ParentGroupId",
                //PeerGroupName: "$PeerGroupName",
                //WorkingVersion: "$WorkingVersion",
                //CompanyName: "$CompanyName",
                //CompanyScore: "$CompanyScore",

                //// copy indicators

                //G_1_1: '$G_1_1',
                //G_1_1_1: '$G_1_1_1',
                //G_1_2: '$G_1_2',
                //G_1_3: '$G_1_3',
                //G_1_3_1: '$G_1_3_1',
                //G_1_3_2: '$G_1_3_2',
                //G_1_3_3: '$G_1_3_3',
                //G_1_3_4: '$G_1_3_4',
                //G_1_3_5: '$G_1_3_5',
                //G_1_4: '$G_1_4',
                //G_1_4_1: '$G_1_4_1',
                //G_1_4_3: '$G_1_4_3',
                //G_1_4_4: '$G_1_4_4',
                //G_1_4_5: '$G_1_4_5',
                //G_1_4_6: '$G_1_4_6',
                //G_1_5: '$G_1_5',
                //G_2_1: '$G_2_1',
                //G_2_2: '$G_2_2',
                //G_2_3: '$G_2_3',
                //G_2_4: '$G_2_4',
                //G_2_5: '$G_2_5',
                //G_2_5_1: '$G_2_5_1',
                //G_2_6: '$G_2_6',
                //G_2_7: '$G_2_7',
                //G_2_8: '$G_2_8',
                //G_2_9: '$G_2_9',
                //G_2_10: '$G_2_10',
                //G_2_11: '$G_2_11',
                //G_2_12: '$G_2_12',
                //G_2_13: '$G_2_13',
                //G_3_1: '$G_3_1',
                //G_3_2: '$G_3_2',
                //G_3_3_1: '$G_3_3_1',
                //G_3_4: '$G_3_4',
                //S_1_1: '$S_1_1',
                //S_1_1_1: '$S_1_1_1',
                //S_1_2: '$S_1_2',
                //S_1_3: '$S_1_3',
                //S_1_4: '$S_1_4',
                //S_1_5: '$S_1_5',
                //S_1_5_1: '$S_1_5_1',
                //S_1_6: '$S_1_6',
                //S_1_6_1: '$S_1_6_1',
                //S_1_6_2: '$S_1_6_2',
                //S_1_6_3: '$S_1_6_3',
                //S_1_6_4: '$S_1_6_4',
                //S_1_6_5: '$S_1_6_5',
                //S_1_6_6: '$S_1_6_6',
                //S_1_7: '$S_1_7',
                //S_2_1: '$S_2_1',
                //S_2_1_1: '$S_2_1_1',
                //S_2_1_2: '$S_2_1_2',
                //S_2_1_3: '$S_2_1_3',
                //S_2_2: '$S_2_2',
                //S_2_2_1: '$S_2_2_1',
                //S_2_2_2: '$S_2_2_2',
                //S_2_2_3: '$S_2_2_3',
                //S_2_2_4: '$S_2_2_4',
                //S_2_3: '$S_2_3',
                //S_3_1_1: '$S_3_1_1',
                //S_3_1_2: '$S_3_1_2',
                //S_3_1_3: '$S_3_1_3',
                //S_3_1_4: '$S_3_1_4',
                //S_3_1_5: '$S_3_1_5',
                //S_3_1_6: '$S_3_1_6',
                //S_3_1_7: '$S_3_1_7',
                //S_3_1_8: '$S_3_1_8',
                //S_3_1_9: '$S_3_1_9',
                //S_3_1_10: '$S_3_1_10',
                //S_3_1_11: '$S_3_1_11',
                //S_3_1_12: '$S_3_1_12',
                //S_3_2_1: '$S_3_2_1',
                //S_3_3: '$S_3_3',
                //S_4_1: '$S_4_1',
                //S_4_2_1: '$S_4_2_1',
                //S_4_2_2: '$S_4_2_2',
                //S_4_2_3: '$S_4_2_3',
                //S_4_2_4: '$S_4_2_4',
                //S_4_2_5: '$S_4_2_5',
                //S_4_2_6: '$S_4_2_6',
                //S_4_2_7: '$S_4_2_7',
                //S_4_2_8: '$S_4_2_8',
                //S_4_2_9: '$S_4_2_9',
                //S_4_2_10: '$S_4_2_10',
                //S_4_2_11: '$S_4_2_11',
                //S_4_2_12: '$S_4_2_12',
                //S_4_2_13: '$S_4_2_13',
                //S_4_2_14: '$S_4_2_14',
                //S_4_3: '$S_4_3',
                //S_5_1: '$S_5_1',
                //S_5_2: '$S_5_2',
                //S_5_3: '$S_5_3',
                //E_1_1: '$E_1_1',
                //E_1_1_1: '$E_1_1_1',
                //E_1_2: '$E_1_2',
                //E_1_2_1: '$E_1_2_1',
                //E_1_2_2: '$E_1_2_2',
                //E_1_2_3: '$E_1_2_3',
                //E_1_2_4: '$E_1_2_4',
                //E_1_2_6: '$E_1_2_6',
                //E_1_2_7: '$E_1_2_7',
                //E_1_2_8: '$E_1_2_8',
                //E_1_3: '$E_1_3',
                //E_1_3_2: '$E_1_3_2',
                //E_1_3_3: '$E_1_3_3',
                //E_1_3_4: '$E_1_3_4',
                //E_1_3_5: '$E_1_3_5',
                //E_1_4: '$E_1_4',
                //E_1_5: '$E_1_5',
                //E_1_6: '$E_1_6',
                //E_1_7: '$E_1_7',
                //E_1_7_1: '$E_1_7_1',
                //E_1_7_2: '$E_1_7_2',
                //E_1_8: '$E_1_8',
                //E_1_9: '$E_1_9',
                //E_1_10: '$E_1_10',
                //E_1_11: '$E_1_11',
                //E_1_12: '$E_1_12',
                //E_2_1: '$E_2_1',
                //E_2_1_1: '$E_2_1_1',
                //E_2_1_2: '$E_2_1_2',
                //E_2_1_3: '$E_2_1_3',
                //E_2_1_4: '$E_2_1_4',
                //E_2_1_5: '$E_2_1_5',
                //E_2_1_6: '$E_2_1_6',
                //E_2_1_7: '$E_2_1_7',
                //E_2_1_8: '$E_2_1_8',
                //E_2_1_9: '$E_2_1_9',
                //E_2_1_10: '$E_2_1_10',
                //E_2_2: '$E_2_2',
                //E_3_1_1: '$E_3_1_1',
                //E_3_1_2: '$E_3_1_2',
                //E_3_1_3: '$E_3_1_3',
                //E_3_1_4: '$E_3_1_4',
                //E_3_1_5: '$E_3_1_5',
                //E_3_1_6: '$E_3_1_6',
                //E_3_1_7: '$E_3_1_7',
                //E_3_1_8: '$E_3_1_8',
                //E_3_1_9: '$E_3_1_9',
                //E_3_1_10: '$E_3_1_10',
                //E_3_1_11: '$E_3_1_11',
                //E_3_1_12: '$E_3_1_12',
                //E_3_1_13: '$E_3_1_13',
                //E_3_1_14: '$E_3_1_14',
                //E_3_1_15: '$E_3_1_15',
                //E_3_1_16: '$E_3_1_16',
                //E_3_1_17: '$E_3_1_17',
                //E_3_2: '$E_3_2',
                //P_1: '$P_1',
                //P_2: '$P_2',
                //P_3: '$P_3',
                //P_4: '$P_4',
                //P_5: '$P_5',
                //P_6: '$P_6',
                //P_7: '$P_7',
                //P_8: '$P_8',
                //P_9: '$P_9',
                //P_10: '$P_10',
                //P_11: '$P_11',
                //P_12: '$P_12',

                // copy themes 
                E: "$E",
                S: "$S",
                G: "$G",
                P: "$P",


                // copy topics 
                E_1: "$E_1",
                E_2: "$E_2",
                E_3: "$E_3",

                G_1: "$G_1",
                G_2: "$G_2",
                G_3: "$G_3",

                S_1: "$S_1",
                S_2: "$S_2",
                S_3: "$S_3",
                S_4: "$S_4",
                S_5: "$S_5",


                CompanyScore: {
                    $add: [
                           { $ifNull: [{ $multiply: [.001, "$E.Score", "$E.Weight"] }, 0] },
                           { $ifNull: [{ $multiply: [.001, "$S.Score", "$S.Weight"] }, 0] },
                           { $ifNull: [{ $multiply: [.001, "$G.Score", "$G.Weight"] }, 0] },
                           { $ifNull: [{ $multiply: [.001, "$P.Score", "$P.Weight"] }, 0] },
                    ]
                }
            }

    }
    ,
    {
        $out: "ScoringMatrixOutput"
    }
]
}


